package org.dromara;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;

import java.nio.charset.StandardCharsets;

public class EncryptResponseBodyWrapper {

    public static void main(String[] args){
        // 公钥
        String publicKey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdHnzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ==";

        //1. 生成秘钥
        String aesPassword = RandomUtil.randomString(32);
        //2. 秘钥使用 Base64 编码
        String encryptAes = Base64.encode(aesPassword, StandardCharsets.UTF_8);
        //3. Rsa 公钥加密 Base64 编码
        RSA rsa = SecureUtil.rsa(null, publicKey);
        String encryptPassword = rsa.encryptBase64(encryptAes, StandardCharsets.UTF_8, KeyType.PublicKey);

        System.out.println("密钥字符串：" + encryptPassword);

        //4. 对内容进行加密
        long timestamp = System.currentTimeMillis() / 1000;
        String originalBody ="{\"username\":\"admin\",\"password\":\"admin123\",\"timeStamp\":"+timestamp+",\"clientId\":\"e5cd7e4891bf95d1d19206ce24a7b32e\",\"grantType\":\"password\"}";
                String requestBody = SecureUtil.aes(aesPassword.getBytes(StandardCharsets.UTF_8)).encryptBase64(originalBody, StandardCharsets.UTF_8);

        System.out.println("加密body数据：" + requestBody);
    }
}
